import requests  
import mysql.connector  
from mysql.connector import Error  
import json  
import os  
import sys  
from datetime import datetime  
  
def load_config(config_path):  
    """Load configuration from JSON file"""  
    try:  
        with open(config_path, 'r', encoding='utf-8') as f:  
            return json.load(f)  
    except FileNotFoundError:  
        print(f"Configuration file {config_path} not found!")  
        sys.exit(1)  
    except json.JSONDecodeError:  
        print(f"Invalid JSON in configuration file {config_path}!")  
        sys.exit(1)  
  
def get_gitlab_repos(base_url, token):  
    """Fetch repositories (projects) from GitLab API (single page since <100)"""  
    headers = {'PRIVATE-TOKEN': token}  
    repos_url = f'{base_url}/projects?per_page=100&page=1'  
  
    print("Fetching repositories...")  
    response = requests.get(repos_url, headers=headers)  
  
    if response.status_code != 200:  
        print(f"Failed to fetch repositories: {response.status_code}")  
        print(response.text)  
        return []  
  
    repos = response.json()  
    print(f"Fetched {len(repos)} repositories.")  
    return repos  
  
def connect_to_db():  
    """Connect to MySQL database"""  
    try:  
        connection = mysql.connector.connect(  
            host='localhost',  
            port=3307,  
            database='gitlog',  
            user='gitlog_admin',  
            password='gitlog_admin'  
        )  
        if connection.is_connected():  
            print("Database connection successful!")  
            return connection  
    except Error as e:  
        print(f"Database connection failed: {e}")  
        return None  
  
def import_repos_to_db(repos, dry_run=False):  
    """Import repositories to repository table or print in dry-run"""  
    if dry_run:  
        print("Dry-run mode: Printing repo data without inserting.")  
        for repo in repos:  
            repo_id = int(repo.get('id', 0)) + 10000  
            print(f"Would insert: ID={repo_id}, Repo Fullname={repo.get('name')}, Path with Namespace={repo.get('path_with_namespace')}, Description={repo.get('description')}, Created At={repo.get('created_at')}, Web URL={repo.get('web_url')}")  
        print(f"Would import {len(repos)} repositories.")  
        return  
  
    connection = connect_to_db()  
    if not connection:  
        return  
  
    cursor = connection.cursor()  
  
    insert_query = """  
    INSERT INTO repository   
    (id, project_id, repo_fullname, path_with_namespace, description, created_at, web_url, updated_at)   
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s)  
    ON DUPLICATE KEY UPDATE  
    repo_fullname = VALUES(repo_fullname),  
    path_with_namespace = VALUES(path_with_namespace),  
    description = VALUES(description),  
    created_at = VALUES(created_at),  
    web_url = VALUES(web_url),  
    updated_at = VALUES(updated_at)  
    """  
  
    inserted_count = 0  
    for repo in repos:  
        try:  
            repo_id = int(repo.get('id', 0)) + 20000  
            project_id = None  # 允许NULL，根据表结构  
            repo_fullname = repo.get('name')  
            path_with_namespace = repo.get('path_with_namespace')  
            description = repo.get('description')  
  
            created_at = None  
            if repo.get('created_at'):  
                try:  
                    created_at = datetime.strptime(repo['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ')  
                except ValueError:  
                    print(f"Invalid created_at format for repo {repo_fullname}")  
  
            web_url = repo.get('web_url')  
            updated_at = datetime.now()  
  
            cursor.execute(insert_query, (  
                repo_id, project_id, repo_fullname, path_with_namespace, description,   
                created_at, web_url, updated_at  
            ))  
            inserted_count += 1  
        except Error as e:  
            print(f"Error inserting repo {repo.get('name')}: {e}")  
  
    connection.commit()  
    cursor.close()  
    connection.close()  
  
    print(f"Imported {inserted_count} repositories successfully.")  
  
def main():  
    # Get config path (relative to this script in data/)  
    script_dir = os.path.dirname(os.path.abspath(__file__))  
    config_path = os.path.join(script_dir, '..', 'gitlab-api-test', 'config.json')  
  
    config = load_config(config_path)  
    env_name = 'jwzh'  # Fixed to local as per request  
    if env_name not in config:  
        print(f"Environment '{env_name}' not found!")  
        sys.exit(1)  
  
    env_config = config[env_name]  
    base_url = env_config['url']  
    token = env_config['token']  
  
    dry_run = '--dry-run' in sys.argv  
    repos = get_gitlab_repos(base_url, token)  
    if repos:  
        import_repos_to_db(repos, dry_run)  
  
if __name__ == "__main__":  
    main() 